package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.NdDevicesAttribute;
import com.ruoyi.system.domain.NdDevicesAttributeLink;
import com.ruoyi.system.domain.Nd_DevicesWrokStatus;
import com.ruoyi.system.mapper.NdDevicesAttributeMapper;
import com.ruoyi.system.mapper.NdDevicesWorkStatusMapper;
import com.ruoyi.system.service.INdAlarmRecorderService;
import com.ruoyi.system.service.INdDevicesAttributeLinkService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.NdDevices_shoes;
import com.ruoyi.system.service.INdDevices_shoesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 铁鞋台账Controller
 *
 * @author lhy
 * @date 2024-06-05
 */
@RestController
@RequestMapping("/system/nddevicesshoes")
public class NdDevices_shoesController extends BaseController {
    @Autowired
    private INdDevices_shoesService ndDevices_shoesService;
    @Autowired
    private INdDevicesAttributeLinkService ndDevicesAttributeLinkService;
    @Resource
    private NdDevicesAttributeMapper ndDevicesAttributeMapper;
    @Resource
    private NdDevicesWorkStatusMapper ndDevicesWorkStatusMapper;
    @Autowired
    private INdAlarmRecorderService ndAlarmRecorderService;


    /**
     * 查询铁鞋台账列表
     */
    @PreAuthorize("@ss.hasPermi('system:nddevicesshoes:list')")
    @GetMapping("/list")
    public TableDataInfo list(NdDevices_shoes ndDevices_shoes) {
        startPage();
        List<NdDevices_shoes> list = ndDevices_shoesService.selectNdDevices_shoesList(ndDevices_shoes);
        return getDataTable(list);
    }

    /**
     * 导出铁鞋台账列表
     */
    @PreAuthorize("@ss.hasPermi('system:nddevicesshoes:export')")
    @Log(title = "铁鞋台账", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NdDevices_shoes ndDevices_shoes) {
        List<NdDevices_shoes> list = ndDevices_shoesService.selectNdDevices_shoesList(ndDevices_shoes);
        ExcelUtil<NdDevices_shoes> util = new ExcelUtil<NdDevices_shoes>(NdDevices_shoes.class);
        util.exportExcel(response, list, "铁鞋台账数据");
    }

    /**
     * 获取铁鞋台账详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:nddevicesshoes:query')")
    @GetMapping(value = "/{deviceId}")
    public AjaxResult getInfo(@PathVariable("deviceId") Long deviceId) {
        return success(ndDevices_shoesService.selectNdDevices_shoesByDeviceId(deviceId));
    }

    /**
     * 新增铁鞋台账
     */
    @PreAuthorize("@ss.hasPermi('system:nddevicesshoes:add')")
    @Log(title = "铁鞋台账", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NdDevices_shoes ndDevices_shoes) {
        ndDevices_shoes.setCreateBy(getUsername());
//        return toAjax(ndDevices_shoesService.insertNdDevices_shoes(ndDevices_shoes));
        ndDevices_shoesService.insertNdDevices_shoes(ndDevices_shoes);
        //添加默认鞋箱
        ndDevicesWorkStatusMapper.insertIntoDeviceWorkStatus(ndDevices_shoes.getDeviceId(), ndDevices_shoes.getDefaultBoxId());
        List<NdDevicesAttribute> ndDevicesAttributes = ndDevicesAttributeMapper.selectNdDevicesAttributeList(new NdDevicesAttribute());
        System.out.println("查询" + ndDevicesAttributes);
        ArrayList<NdDevicesAttributeLink> ndDevicesAttributeLinks = new ArrayList<>();
        for (NdDevicesAttribute ndDevicesAttribute : ndDevicesAttributes) {
            NdDevicesAttributeLink devicesAttributeLink = NdDevicesAttributeLink.builder()
                    .attributeId(ndDevicesAttribute.getAttributeId())
                    .deviceId(ndDevices_shoes.getDeviceId())
                    .attributeValue("0")
                    .build();
            ndDevicesAttributeLinks.add(devicesAttributeLink);
        }
        return toAjax(ndDevicesAttributeLinkService.insterBach(ndDevicesAttributeLinks));
    }

    /**
     * 修改铁鞋台账
     */
    @PreAuthorize("@ss.hasPermi('system:nddevicesshoes:edit')")
    @Log(title = "铁鞋台账", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NdDevices_shoes ndDevices_shoes) {
        ndDevices_shoes.setUpdateBy(getUsername());
        ndDevices_shoesService.updateNdDevices_shoes(ndDevices_shoes);
        ndDevicesWorkStatusMapper.updateDeviceWorkStatus(ndDevices_shoes.getDeviceId());
        Nd_DevicesWrokStatus ndDevicesWorkStatus = ndDevicesWorkStatusMapper.selectByDeviceId(ndDevices_shoes.getDeviceId());
        if (ndDevicesWorkStatus.Inbox_Id.equals(ndDevicesWorkStatus.default_box_id.toString())){
            //解除
            ndAlarmRecorderService.updateAlaerStatus(10, Integer.valueOf(ndDevicesWorkStatus.Device_Id)
                    , Integer.parseInt(ndDevicesWorkStatus.Station_Id), ndDevicesWorkStatus.Device_VIN);
            Nd_WebSocketController myWebSocketHandler = new Nd_WebSocketController();
            myWebSocketHandler.sendMessage("devChange");
        }
        return toAjax(1);
    }

    /**
     * 删除铁鞋台账
     */
    @PreAuthorize("@ss.hasPermi('system:nddevicesshoes:remove')")
    @Log(title = "铁鞋台账", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deviceIds}")
    public AjaxResult remove(@PathVariable Long[] deviceIds) {
        for (Long deviceId : deviceIds) {
            ndDevicesWorkStatusMapper.deleteDevice(deviceId);
        }
        return toAjax(ndDevices_shoesService.deleteNdDevices_shoesByDeviceIds(deviceIds));
    }
}
